package com.kongge.viewdisplayframeworklib.model;

import android.content.Context;
import android.util.Log;
import android.util.Xml;

import com.google.gson.Gson;
import com.kongge.viewdisplayframeworklib.control.GlobalMagager;
import com.kongge.viewdisplayframeworklib.control.PublicInterface;
import com.kongge.viewdisplayframeworklib.model.queue.PageQueueNode;
import com.kongge.viewdisplayframeworklib.util.FileUtil;

import org.xmlpull.v1.XmlPullParser;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class PageNodeManager {

	private Map<Integer, PageNode> mNodesMap;
	private Map<Integer, PageStackList> mPageStacksMap;
	private ArrayList<TabberBarItem> mTabberBarItems;

	public int GetTabberBarItemCount() {
		return mTabberBarItems.size();
	}

	public TabberBarItem getTabberBarItemByIndex(int nIndex) {
		if (nIndex >= 0 && nIndex < mTabberBarItems.size()) {
			return mTabberBarItems.get(nIndex);
		}
		return null;
	}

	public PageNodeManager() {
		mNodesMap = new HashMap<Integer, PageNode>();
		mPageStacksMap = new HashMap<Integer, PageStackList>();
		mTabberBarItems = new ArrayList<TabberBarItem>();
	}

	public PageNode GetPageNodeById(int nPageId) {
		PageNode node = mNodesMap.get(nPageId);
		return node;
	}

	public PageStackList getPageStackListByStackId(int nStack) {
		return mPageStacksMap.get(Integer.valueOf(nStack));
	}

	public void init(String configPath) {

		String configStr = FileUtil.parserStreamToString(FileUtil.parserAssertFile(GlobalMagager.getGlobalActivity(), configPath));
		Gson gson = new Gson();
		Map<String, Object> configMap = (Map<String, Object>) gson.fromJson(configStr, Object.class);

		List<Object> tabbarList = (List<Object>) configMap.get("tabbar");
		for (int i = 0; i < tabbarList.size(); i++) {
			Map<String, Object> tabItemMap = (Map<String, Object>) tabbarList.get(i);
			TabberBarItem item = new TabberBarItem();
			item.mStack = Integer.parseInt((String) tabItemMap.get("stack"));
			item.mName = (String) tabItemMap.get("name");
			item.mImage = (String) tabItemMap.get("image");
			item.mLink = Integer.parseInt((String) tabItemMap.get("linkid"));
			mTabberBarItems.add(item);
		}

		List<Object> stackList = (List<Object>) configMap.get("stacklist");
		for (int i = 0; i < stackList.size(); i++) {
			Map<String, Object> stackItemMap = (Map<String, Object>) stackList.get(i);
			PageStackList pageStackList = new PageStackList();
			pageStackList.SetStacklistId(Integer.parseInt((String) stackItemMap.get("id")));
			pageStackList.SetStackList((String) stackItemMap.get("list"));
			putStackListIntoMaps(pageStackList);
		}

		List<Object> pageList = (List<Object>) configMap.get("page");
		for (int i = 0; i < pageList.size(); i++) {
			Map<String, Object> pageItemMap = (Map<String, Object>) pageList.get(i);
			PageNode node = new PageNode();
			node.setPageId(Integer.parseInt((String) pageItemMap.get("id")));
			node.setStackLevel(Integer.parseInt((String) pageItemMap.get("stackLevel")));
			node.setPageLayoutName((String) pageItemMap.get("layoutName"));
			node.setTitle((String) pageItemMap.get("title"));
			if (pageItemMap.containsKey("queueid")) {
				node.setQueueId(Integer.parseInt((String) pageItemMap.get("queueid")));
			}
			putNodeIntoMaps(node);
		}

		List<Object> pageQueueList = (List<Object>) configMap.get("pageQueue");
		for (int i = 0; i < pageQueueList.size(); i++) {
			Map<String, Object> pageQueueItem = (Map<String, Object>) pageQueueList.get(i);
			PageQueueNode pageQueueNode = new PageQueueNode();
			pageQueueNode.setPageId(Integer.parseInt((String) pageQueueItem.get("id")));
			pageQueueNode.setStackLevel(Integer.parseInt((String) pageQueueItem.get("stackLevel")));
			pageQueueNode.setPageLayoutName((String) pageQueueItem.get("layoutName"));
			pageQueueNode.setTitle((String) pageQueueItem.get("title"));
			pageQueueNode.setDefaultFocus(Integer.parseInt((String) pageQueueItem.get("foucs")));
			List<Map<String, Object>> framesList = (List<Map<String, Object>>) pageQueueItem.get("frames");
			List<PageNode> subPageNodes = new LinkedList<>();
			for (int j = 0; j < framesList.size(); j++) {
				Map<String, Object> frameItem = framesList.get(j);
				int pageid = Integer.parseInt((String) frameItem.get("pageid"));
				String title = (String) frameItem.get("title");
				PageNode subPageNode = mNodesMap.get(pageid);
				subPageNode.setTitle(title);
				subPageNodes.add(subPageNode);
			}
			pageQueueNode.setSubPageNodeList(subPageNodes);
			putNodeIntoMaps(pageQueueNode);
		}
	}

	private void putNodeIntoMaps(PageNode node) {
		if (node != null) {
			mNodesMap.put(node.getPageId(), node);
		}
	}

	private void putStackListIntoMaps(PageStackList pageStackList) {
		if (pageStackList != null) {
			mPageStacksMap.put(pageStackList.GetStacklistId(), pageStackList);
		}
	}

}
